import React from "react";
import loadable from '../utils/loadable'
import {
    UserOutlined, DownOutlined, AppstoreOutlined, GiftOutlined, FileExcelOutlined,
    DownloadOutlined, DislikeOutlined, EnvironmentOutlined, ExperimentOutlined, DeleteColumnOutlined,
    DatabaseOutlined, CustomerServiceOutlined, CoffeeOutlined, CarryOutOutlined, CameraOutlined,
    BulbOutlined, BranchesOutlined,BellOutlined,BarsOutlined,AudioOutlined,AuditOutlined
} from '@ant-design/icons';

// state 存储数据
const menuLists = [];

export const menuReducer = (state=menuLists, action) => {
    switch(action.type){
        case 'listStart':
            return state;
        case 'menuEnd':
            let list = formatRoutes(action.list);
            return [...list]
        default:
            return state;
    }
}


const UserList = loadable(() => import('../components/user/list.js'))
const UserAdd = loadable(() => import('../components/user/add.js'))
const ManagerList = loadable(() => import('../components/manager/list.js'))
const ManagerAdd = loadable(() => import('../components/manager/add.js'))
const Errors = loadable(() => import('../components/Errors'))


/* 格式化路由菜单 */
const formatRoutes = (menuRoutes) => {
    let newMenuRoutes = [];
    menuRoutes.forEach(route => {
      // 如果有子路由，递归调用函数本身，继续映射
      let children;
      if (route.children && route.children instanceof Array) {
        children = formatRoutes(route.children);
      }

      const {
        path,
        name,
        meta,
        hidden
      } = route;
  
      // 组件映射
      let newRoute = {
        path,
        name,
        meta,
        iconCls:()=>{
            switch (route.iconCls) {
                case "el-icon-s-custom":
                  return <UserOutlined/>;
                case "el-icon-s-goods":
                  return <DownOutlined/>;
                case "el-icon-user-solid":
                  return <AppstoreOutlined/>;
                case "el-icon-s-cooperation":
                  return <GiftOutlined/>;
                case "el-icon-s-order":
                  return <FileExcelOutlined/>;
                case "el-icon-s-data":
                  return <DownloadOutlined/>;
                case "el-icon-s-order":
                  return <DislikeOutlined/>;
                case "el-icon-s-claim":
                  return <EnvironmentOutlined/>;
                case "el-icon-s-data":
                  return <ExperimentOutlined/>;
                case "el-icon-s-custom":
                  return <DeleteColumnOutlined/>;
                case "el-icon-s-claim":
                  return <DatabaseOutlined/>;
                case "el-icon-service":
                  return <CustomerServiceOutlined/>;
                case "el-icon-microphone":
                  return <CoffeeOutlined/>;
                case "el-icon-mic":
                  return <CarryOutOutlined/>;
                case "el-icon-s-goods":
                  return <CameraOutlined/>;
                case "el-icon-s-order":
                  return <BulbOutlined/>;
                case "el-icon-s-cooperation":
                  return <BranchesOutlined/>;
                case "el-icon-s-help":
                  return <BellOutlined/>;
                case "el-icon-platform-eleme":
                  return <DislikeOutlined/>;
                case "el-icon-office-building":
                  return <BarsOutlined/>;
                case "el-icon-box":
                  return <AudioOutlined/>;
                case "el-icon-edit":
                  return <AuditOutlined/>;
              }
        },
        hidden,
        children,
        component() {
          switch (route.component) {
            // case "Main":
            //   return loadable(()=>import("../components/Main"));
            case "UserList":
              return <UserList/>;
            case "UserAdd":
              return <UserAdd/>;
            case "ProductList":
              return <Errors/>;
            case "ProductAdd":
              return <Errors/>;
            case "LogList":
              return <Errors/>;
            case "LogAdd":
              return <Errors/>;
            case "ManagerList":
              return <ManagerList/>;
            case "MusicAdd":
              return <Errors/>;
            case "MusicList":
              return <Errors/>;
            case "ProjectList":
              return <Errors/>;
            case "ShopList":
              return <Errors/>;
            case "ShopAdd":
              return <Errors/>;
            case "BannerList":
              return <Errors/>;
            case "BannerAdd":
              return <Errors/>;
            case "CategoryList":
              return <Errors/>;
            case "CategoryAdd":
              return <Errors/>;
          }
        }
      };
      newMenuRoutes.push(newRoute);
    })
    return newMenuRoutes;
  }
  